<?php

include_once("dbconnect.php");

if (isset($_POST['id'])) {
    $id = ($_POST['id']);
    $query = "DELETE FROM `products` WHERE name='$id'";
    mysqli_query(getConnect(), $query);
    echo $id;
}
if (isset($_POST['name'])) {
    $name = ($_POST['name']);

    $price = ($_POST['price']);
    $quantity = ($_POST['quantity']);
    $cate = ($_POST['cate']);
    $manu = ($_POST['manu']);
    $ct = getCate($cate);
    $mn = getManu($manu);
    $pId = ($_POST['id']);
    $query = "UPDATE  `products` SET name = '$name' , price = $price ,quantity= $quantity,category= $ct,manufacturer= $mn where id ='$pId' ";
    if (!mysqli_query(getConnect(), $query)) {
        die('Error: ');
    }
    echo "OK";
}

function getCate($cat) {
    $query = "select * FROM `categories` WHERE name='$cat'";
    $result = mysqli_query(getConnect(), $query);
    while ($row = mysqli_fetch_array($result)) {
        return $row['id'];
    }
}

function getManu($man) {
    $query = "select * FROM `manufacturers` WHERE name='$man'";
    $result = mysqli_query(getConnect(), $query);
    while ($row = mysqli_fetch_array($result)) {
        return $row['id'];
    }
}

if (isset($_POST['cateToManu'])) {
    $str = "";
    $ct_id = ($_POST['cateToManu']);
    $type = ($_POST['type']);
    $sql_query = "select manufacturers.name from manufacturers 
inner join cate_manu on manufacturers.id = cate_manu.manufacturer
inner join categories on categories.id = cate_manu.category
where categories.name = '$ct_id'";

    $result = mysqli_query(getConnect(), $sql_query);
    if ($type == "1") {
        $str = $str . "           <select id ='proManu' class='form-control'>";
    } else if ($type == "2") {
        $str = $str . "           <select id ='newProManu' class='form-control'>";
    }
    while ($row = mysqli_fetch_array($result)) {
        $str = $str . "<option>${row['name']}</option>";
    }
    $str = $str . "  </select>";
    echo $str;
}
if (isset($_POST['new'])) {
    $re = "00";
    $newManu = trim(strtoupper($_POST['new']));
    $result = mysqli_query(getConnect(), "Select * from `manufacturers`");
    while ($row = mysqli_fetch_array($result)) {

        if ($newManu == strtoupper($row['name'])) {
            $re = "1";
        }
    }
    echo $re;
}

if (isset($_POST['C_newName'])) {

    $name = $_POST['C_newName'];
    $price = $_POST['C_newPrice'];
    $quantity = $_POST['C_newQuantity'];
    $cate = $_POST['C_newCate'];
    $manu = $_POST['C_newManu'];
    $code = $_POST['C_newCode'];
    $ct = getCate($cate);
    $desc = $_POST['C_desc'];
    $mn = getManu($manu);
    $query = "INSERT INTO  `products`(id,name,price,quantity,category,manufacturer,`desc`) VALUES ($code,'$name','$price','$quantity','$ct','$mn','$desc')";
    echo $query;
    if (!mysqli_query(getConnect(), $query)) {
        die(mysql_error());
    }
    echo "OK";
}

function getlistCate() {
    $str = "";
    $result = mysqli_query(getConnect(), "SELECT * FROM `categories` ");
    $str = $str . "<option>Chọn danh mục</option>";
    while ($row = mysqli_fetch_array($result)) {
        $str = $str . "<option>${row['name']}</option>";
    }
    echo $str;
}

function getlistManu() {
    $str = "";
    $result = mysqli_query(getConnect(), "SELECT * FROM `manufacturers` ");
    $str = $str . "<option>Chọn nhà cung cấp</option>";
    while ($row = mysqli_fetch_array($result)) {
        $str = $str . "<option>${row['name']}</option>";
    }
    echo $str;
}
function convertPrice($price){
    $str ="";
    $count = strlen($price);
    while ($count > 3){
        $tmp = substr($price, $count-3, 3);
        $str = ",".$tmp.$str;
        $count = $count -3;
    }
    
    $str = "" .substr($price, 0, $count).$str;
    return $str;
}
function displayBody($role) {
    $str = "<tbody>";
    $result = mysqli_query(getConnect(), "SELECT  distinct products.id,products.name, products.price, products.quantity ,categories.name
        as cate,manufacturers.name as manu FROM `products` inner join categories on
        products.category = categories.id inner join manufacturers on products.manufacturer = manufacturers.id");
    while ($row = mysqli_fetch_array($result)) {
        $priCon = convertPrice($row['price']);
        $str = $str . " <tr class='odd gradeX'>
                       <td style='width:36%;vertical-align:middle;'>${row['name']}</td>
                            <td style='width:8%;vertical-align:middle;text-align:center'>$priCon</td>
                            <td style='width:8%;vertical-align:middle;text-align:center'>${row['quantity']}</td>
                            <td style='width:13%;vertical-align:middle;text-align:center'>${row['cate']}</td>
                            <td style='width:13%;vertical-align:middle;text-align:center'>${row['manu']}</td>";
        if ($role == 1) {
            $str = $str . "<td class='center' style='text-align:center;vertical-align:middle;width:1%'>
                           <button  id ='${row['id']}' type='button' data-target='#myModal' data-toggle='modal' class='btn btn-link' value ='edit'> <i class='fa fa-wrench fa-fw'></i></button></td>
                           <td class='center' style='text-align:center;vertical-align:middle;width:1%'>
                           <button type='button' class='btn btn-link' value ='del'><i class='fa fa-bitbucket'></i></button></td>
                           </tr>";
        } elseif ($role == 2) {
            $str = $str . " <td class='center' style='text-align:center;vertical-align:middle;width:1%;display:none'>
                            <i class='fa fa-wrench fa-fw'></i>
                            <button  id ='${row['id']}' type='button' data-target='#myModal' data-toggle='modal' class='btn btn-link' value ='edit'>Sửa</button></td>
                            <td class='center' style='text-align:center;vertical-align:middle;width:1%;display:none' >
                            <i class='fa fa-bitbucket'></i>
                            <button  type='button' class='btn btn-link' value ='del'>Xóa</button></td>
                            </tr>";
        }
    }
    $str = $str . " </tbody>";
    echo $str;
}

?>
